package com.wenhao.newcode.shopee.interview;

import java.util.Scanner;

/**
 * @author Wenhao Tong
 * @create 2021-08-02 18:52
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

    }
    /**
     * Note: 类名、方法名、参数名已经指定，请勿修改
     *
     *
     *
     * @param inxml string字符串 xml字符串
     * @param path string字符串 取值路径
     * @return string字符串
     */
    public String GetXMLValue(String inxml, String path) {
        String[] split = path.split(".");
        // write code here
        int index = 0;
        int lo = 0;
        int hi = inxml.length();
        while (lo < hi) {
            if (lo == '<' && hi == '>') {
                lo++;
                hi--;
                String tagLeft = new String();
                String tagRight = new String();
                while (inxml.charAt(lo) == '>' || inxml.charAt(hi) == '/') {
                    tagLeft = tagLeft + inxml.charAt(lo++);
                    tagRight = inxml.charAt(hi--) + tagRight;
                }
                if (inxml.charAt(lo - 1) != '>' || inxml.charAt(hi + 1) != '/' || inxml.charAt(hi) != '<') {
                    return "";
                } else {
                    if (split[index].equals(tagLeft) && split[index].equals(tagRight)) {
                        index++;
                        continue;
                    } else {
                        return "";
                    }
                }
            }
            if (lo != '<' && hi != '>' && index == split.length) {
                return inxml.substring(lo,hi + 1);
            } else {
                return "";
            }
        }
        return "";
    }

    /**
     * Note: 类名、方法名、参数名已经指定，请勿修改
     *
     *
     * find balanced index for an input array
     * @param inputArray int整型一维数组 the input array
     * @return int整型
     */
    public int findBalancedIndex(int[] inputArray) {
        // write code here
        int[] preSum = new int[inputArray.length];
        int[] postSum = new int[inputArray.length];
        preSum[0] = inputArray[0];
        for (int i = 1;i < inputArray.length;i++) {
            preSum[i] = preSum[i - 1] + inputArray[i];
        }
        postSum[inputArray.length - 1] = inputArray[inputArray.length - 1];
        for (int i = inputArray.length - 2;i > -1;i--) {
            postSum[i] = postSum[i + 1] + inputArray[i];
        }
        for (int i = 0;i < inputArray.length;i++) {
            if (preSum[i] == postSum[i]) {
                return i;
            }
        }
        return -1;
    }

}
